<template>
  <!-- 添加或修改线索管理对话框 -->
  <el-dialog :title="form.id?'编辑课程' : '添加课程'" :visible="visible" width="618px" append-to-body :close-on-click-modal="false" @close="closeDailog">
    <el-form ref="form" class="cus-search-box cus-dialog-form-box" label-width="110px" :model="form" :rules="rules">
      <el-form-item label="课程学科" prop="subject">
        <el-select v-model="form.subject" filterable placeholder="请选择学科">
          <el-option
            v-for="e in subjectList"
            :key="e.dictValue"
            :label="e.dictLabel"
            :value="e.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课程名称：" prop="name">
        <el-input v-model="form.name" placeholder="请输入课程名称：" />
      </el-form-item>
      <el-form-item label="适应人群：" prop="applicablePerson">
        <el-select v-model="form.applicablePerson" filterable placeholder="请选择适应人群">
          <el-option
            v-for="item in peoList"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课程价格：" prop="price">
        <el-input-number v-model="form.price" dir="rtl" :precision="0" :min="1" :max="5000" :step="1" placeholder="请输入价格" controls-position="right" />
      </el-form-item>
      <el-form-item label="课程介绍：" prop="info">
        <el-input
          v-model="form.info"
          type="textarea"
          placeholder="请输入内容"
          maxlength="20"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button class="cus-reset-btn" @click="closeDailog">取 消</el-button>
      <el-button class="cus-search-btn" type="primary" @click="btn">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { editCourseApi, addCourseApi } from '@/api/Course'
import { Message } from 'element-ui'
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {
        subject: '', // 学科
        name: '', // 课程名称
        applicablePerson: '', // 适应人群
        price: undefined, // 课程价格
        info: ''
      },
      rules: {
        subject: [{ required: true, message: '请选择课程学科！', trigger: 'blur' }],
        name: [{ required: true, message: '请输入课程名称！', trigger: 'blur' }],
        applicablePerson: [{ required: true, message: '请选择适应人群！', trigger: 'blur' }],
        price: [{ required: true, message: '请输入课程价格！', trigger: 'blur' }]
      },
      // 学科
      subjectList: [],
      // 活动信息列表
      peoList: []
    }
  },
  created() {
  },
  methods: {
    /**
     * 确定点击事件
     */
    btn() {
      this.$refs.form.validate(async key => {
        if (!key) return
        if (this.form.id) {
          // 编辑
          const res = await editCourseApi(this.form)
          if (res.code === 500) {
            Message.error('编辑失败！')
            return
          }
        } else {
          // 新增
          const res = await addCourseApi(this.form)
          if (res.code === 500) {
            Message.error('编辑失败！')
            return
          }
        }
        Message.success(this.form.id ? '编辑成功！' : '添加成功！')
        this.$parent.getCourseList()
        this.closeDailog()
      })
    },
    /**
     * 关闭弹窗
     */
    closeDailog() {
      this.$emit('update:visible', false)
      this.$refs.form.resetFields()
      this.form = {
        subject: '', // 学科
        name: '', // 课程名称
        applicablePerson: '', // 适应人群
        price: undefined, // 课程价格
        info: ''
      }
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-form-item__label{
    font-weight: 500;
    color: #333!important;
}
::v-deep .el-input-number{
    width: 300px;
}
::v-deep .el-input-number .el-input__inner {
  text-align: left; //位置居左
}
::v-deep .el-dialog__header{
  background: #8DF;
}
::v-deep .el-textarea__inner{
  width: 300px !important;
  height: 120px;
}
::v-deep .el-textarea{
  width: 300px !important;
}
</style>
